package com.sword.invoice.channel.lq.utils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;


/**
 * 
 * <p>
 * base64 helper
 * </p>
 * 
 * @author lichunhui lichunhui1314@126.com
 * @version 1.0 Created on 2014-11-19 下午11:15:41
 */
public class Base64Helper {

	private static final String CHARSET = StandardCharsets.UTF_8.toString();

	/**
	 * <p>
	 * 解base64
	 * </p>
	 * 
	 * @param res
	 * @return String
	 * @author: lichunhui lichunhui1314@126.com
	 * @date: Created on 2014-11-17 上午11:17:06
	 */
	public static String encode(String res) {
		try {
			Base64.Encoder encoder = Base64.getEncoder();
			if (res != null && !"".equals(res)) { // 即将解密串不为null并且不为“”
				return new String(encoder.encode(res.getBytes(CHARSET)));
			} else {
//				log.warn("即将解密串为null或者为“” ");
				return "";
			}
		} catch (Exception e) {
//			log.error("未知：" + e);
			return "";
		}
	}

	/**
	 * 
	 * <p>
	 * base64编码
	 * </p>
	 * 
	 * @param res
	 * @return byte[]
	 * @author: lichunhui lichunhui1314@126.com
	 * @date: Created on 2014-11-17 上午11:41:19
	 */
	public static byte[] encode(byte[] res) {
		try {
			Base64.Encoder base = Base64.getEncoder();
			return base.encode(res);
		} catch (Exception e) {
//			log.error("未知：" + e);
			return null;
		}
	}
	
	/**
	 * 
	 * <p>
	 * base64编码
	 * </p>
	 * 
	 * @return byte[]
	 * @author: lichunhui lichunhui1314@126.com
	 * @date: Created on 2014-11-17 上午11:41:19
	 */
	public static String encode(String str, String charset) {
		try {
//			BASE64Encoder base64=new BASE64Encoder();
//			byte[] caByte=str.getBytes(charset);
//			return base64.encode(caByte);
			
			return new String(Base64.getEncoder().encode(str.getBytes(charset)));
		} catch (Exception e) {
//			log.error("未知：" + e);
			return null;
		}
	}

	/**
	 * 
	 * <p>
	 * 解base64编码
	 * </p>
	 * 
	 * @param str
	 * @return
	 * @author: lichunhui lichunhui1314@126.com
	 * @date: Created on 2014-11-17 上午11:41:41
	 */
	public static String decode(String str, String charset) {
		try {
			return new String(Base64.getDecoder().decode(str.getBytes()), charset);
		} catch (Exception e) {
//			log.error("未知：" + e);
			return null;
		}
	}

	public static byte[] decodeByte(byte[] str) {
		return Base64.getDecoder().decode(str);
	}

	/**
	 * 
	 * <p>
	 * 解base64编码
	 * </p>
	 * 
	 * @param str
	 * @return
	 * @throws UnsupportedEncodingException
	 *             byte[]
	 * @author: lichunhui lichunhui1314@126.com
	 * @date: Created on 2014-11-17 上午11:41:58
	 */
	public static byte[] decode(byte[] str) throws UnsupportedEncodingException {
		return Base64.getDecoder().decode(str);
	}
	
	public static void main(String[] args) throws UnsupportedEncodingException {
//		String str="<requestDzfpcx>" + 
//				"<fpqqlsh>00000100000301975313236198987698</fpqqlsh>" + 
//				"<nsrsbh>440001999999260</nsrsbh>" + 
//				"<wbxt>01</wbxt>" + 
//				"</requestDzfpcx>";
//		
//		System.out.println(encode(str,"utf-8"));

		String s="H4sIAAAAAAAAAF2RzU7CQBSFX4XMmphOS6F1R0TewRg3UGuD1CBDYq0x4ScY+SvGoEhS2RiVGBNQFkBVfJlOC2/hnUI0sYvp3Pac7865c4YMoh6RPElpaBNhDvN8PJ5MxhNibCshbKMw0jJkH21icUOWw8g01Zyig5LnOfbgaESMgaignJKsEfwJhaAmahaUUO1AYWiqYsKezr7dqe3bJdjQSZ9Oy/7Ni//k0KZDqzXvzqLN7mIyFCXanoDtQFf1NLO1LHfqePWiZw89u7bsXdHqK21PV5KA7E3eqD0G4HLeo+9dBrwv+53R2lr8WFYGtN4Hn/f4Cb7DNETi4J3Lg9u3Gq7TCaJmWFReCMi/U5FxJMIJEFaG1ILE4yho1VzODPJymEUkq7P6ztgfWK7jBF8Bfww8NiNJkjCGgYVRhmgZxWC5Hmbu14i2S15j7rWatFrxrdvF8zWs9PIC7CkWDa2GrrFWK4ooIEZRyd+4daNAgkAMDh0FKbgt7cSARrt77LTa/1tbsyR0/gN9B4sfBAIAAA==";
		String d=decode(s,"UTF-8");
		System.out.println(d);
	}
}
